package com.dji.custom.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.dji.custom.dao.IExpandWaylinePointMapper;
import com.dji.custom.model.entity.ExpandWaylinePointEntity;
import com.dji.custom.service.IExpandWaylinePointService;
import com.dji.custom.util.CustomUtil;
import com.dji.custom.util.DateUtils;
import com.dji.sample.component.redis.RedisConst;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sdk.cloudapi.device.DroneModeCodeEnum;
import com.dji.sdk.cloudapi.device.OsdRcDrone;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 飞行记录拓展（Poilt上云）Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-02
 */
@Service
@Slf4j
public class ExpandWaylinePointServiceImpl implements IExpandWaylinePointService
{
    @Autowired
    private IExpandWaylinePointMapper expandWaylinePointMapper;

    @Override
    public int insertExpandWaylinePointCustom(DeviceDTO device, OsdRcDrone data) {
        String deviceSn = device.getDeviceSn();
        Double longitude = Double.valueOf(data.getLongitude());
        Double latitude = Double.valueOf(data.getLatitude());
        DroneModeCodeEnum modeCode = data.getModeCode();
        // 生成临时任务Id
        String jobIdTmp = "";
        try {
            // 任务id = sn码 + 年月日时 + 随机数 保存redis 5分钟 key为sn码
            // 根据sn码 获取jobid，没有的话缓存5分钟
            Object jobIdObj = RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_KEY + deviceSn);
            if (ObjectUtil.isEmpty(jobIdObj)) {
                // 没有直播 则新生成一个jobId 保存
                jobIdTmp = deviceSn + DateUtils.getTimeNum() + CustomUtil.fastSimpleUUID();
            } else {
                jobIdTmp = String.valueOf(jobIdObj);
            }
            RedisOpsUtils.setWithExpire(RedisConst.DEVICE_ONLINE_KEY + deviceSn, jobIdTmp, RedisConst.DEVICE_ALIVE_SECOND);
        } catch (Exception e) {
            log.error("------保存飞机任务在线id失败-------{}", e.getMessage());
            e.printStackTrace();
        }
        // 保存经纬度记录
        try {
            if (0 != longitude && 0 != latitude
                    && DroneModeCodeEnum.IDLE != modeCode
                    && DroneModeCodeEnum.TAKEOFF_PREPARE != modeCode
                    && DroneModeCodeEnum.TAKEOFF_FINISHED != modeCode) {
                //使用Date
                Date d = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("ss");
                String timestr = sdf.format(d);
                timestr = timestr.substring(timestr.length() - 1, timestr.length());
                //判断是否为需要的时间，比如整点时分钟为00
                if (timestr.equals("4") || timestr.equals("5") || timestr.equals("6")
                        || timestr.equals("9") || timestr.equals("10") || timestr.equals("11")) {
                    log.info("******************飞机的实时经纬度为 {} ,{}", longitude, latitude);
                    // 保存节点数据 联合索引 任务id+经纬度不重复
                    ExpandWaylinePointEntity expandWaylinePointInsert
                            = new ExpandWaylinePointEntity(deviceSn, jobIdTmp, longitude, latitude);
                    int insertNum = expandWaylinePointMapper.insert(expandWaylinePointInsert);
                    System.out.println("insertNum = "+insertNum);
                    if(insertNum == 1){
                        // 添加成功则保存最新的任务id
                        RedisOpsUtils.setWithExpire(RedisConst.DEVICE_LAST_JOB_KEY + deviceSn, jobIdTmp, RedisConst.DEVICE_ONLINE_ALIVE_SECOND);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------保存300机型的路线数据失败-------{}", e.getMessage());
        }
        return 0;
    }
}
